package com.dong.auth.security.matcher;

import org.springframework.http.HttpMethod;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;

import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

public class AntPathRequestMatcher implements RequestMatcher{
    private static final String MATCH_ALL = "/**";

    private final String pattern;

    private final HttpMethod httpMethod;

    private final UrlPathHelper urlPathHelper;

    private final AntPathMatcher antPathMatcher ;

    private final List<String> exclusiveUrls ;

    private final static String defaultExclusiveUrls = "/static/**" ;

    public AntPathRequestMatcher() {
        this(MATCH_ALL);
    }

    public AntPathRequestMatcher(String pattern) {
        this(pattern,null);
    }

    public AntPathRequestMatcher(String pattern, String httpMethod){
        this(pattern,httpMethod,null);
    }
    public AntPathRequestMatcher(String pattern, String httpMethod, UrlPathHelper urlPathHelper) {
        this(pattern, httpMethod, urlPathHelper, Collections.singletonList(defaultExclusiveUrls));
    }

    public AntPathRequestMatcher(String pattern, String httpMethod, UrlPathHelper urlPathHelper, List<String> exclusiveUrls) {
        if (null == exclusiveUrls || exclusiveUrls.isEmpty()){
            exclusiveUrls = Collections.singletonList(defaultExclusiveUrls) ;
        }
        this.exclusiveUrls = exclusiveUrls;
        Assert.hasText(pattern, "Pattern cannot be null or empty");
        if (pattern.equals(MATCH_ALL) || pattern.equals("**")) {
            pattern = MATCH_ALL;
        }
        this.pattern = pattern;
        this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
        this.urlPathHelper = urlPathHelper;
        this.antPathMatcher = new AntPathMatcher();
    }

    @Override
    public boolean matches(HttpServletRequest request) {
        //是否是不拦截的请求
        String url = getRequestPath(request);
        for (String exclusiveUrl : this.exclusiveUrls){
            if (this.antPathMatcher.match(exclusiveUrl,url)){
                return false ;
            }
        }
        if (this.httpMethod != null && StringUtils.hasText(request.getMethod()) && this.httpMethod != valueOf(request.getMethod())) {
            return false;
        }
        if (this.pattern.equals(MATCH_ALL)) {
            return true;
        }

        return this.antPathMatcher.match(this.pattern,url);
    }

    private String getRequestPath(HttpServletRequest request) {
        if (this.urlPathHelper != null) {
            return this.urlPathHelper.getPathWithinApplication(request);
        }
        String url = request.getServletPath();
        String pathInfo = request.getPathInfo();
        if (pathInfo != null) {
            url = StringUtils.hasLength(url) ? url + pathInfo : pathInfo;
        }
        return url;
    }
    private static HttpMethod valueOf(String method) {
        try {
            return HttpMethod.valueOf(method);
        }
        catch (IllegalArgumentException ex) {
            return null;
        }
    }

    public static void main(String[] args) {
        AntPathRequestMatcher antPathRequestMatcher = new AntPathRequestMatcher("/static/**");
    }
}
